package com.ginger.demo.servlet;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AsnycServlet
 */
@WebServlet(asyncSupported = true, description = "异步Servlet", urlPatterns = { "/AsnycServlet" })
public class AsnycServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AsnycServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		long t1 = System.currentTimeMillis();

		// 开启异步
		AsyncContext asyncContext = request.startAsync();

		// 执行业务代码
		CompletableFuture.runAsync(() -> doSomeThing(asyncContext,
				asyncContext.getRequest(), asyncContext.getResponse()));

		System.out.println("async use:" + (System.currentTimeMillis() - t1));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	private void doSomeThing(AsyncContext asyncContext,
			ServletRequest servletRequest, ServletResponse servletResponse) {

		// 模拟耗时操作
		try {
			TimeUnit.SECONDS.sleep(5);
		} catch (InterruptedException e) {
		}

		//
		try {
			servletResponse.getWriter().append("done");
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 业务代码处理完毕, 通知结束
		asyncContext.complete();
	}

}
